//
//  FilterV.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/22.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "FilterV.h"
#import "FilterReusableView.h"
#import "FilterCell.h"
#import "IGArtLibraryApi.h"

@interface FilterV ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSArray *titleArr;
//    NSMutableArray *staueArr;//0,1,2
//    BOOL isShowMore;
//    BOOL isShowMore2;
    UILabel *titleLabel;
}
@property (nonatomic, strong)UIView *hiddenView;
@property (nonatomic, strong)UIView *backView;
@property (nonatomic, strong)UIButton *resetBtn;
@property (nonatomic, strong)UIButton *sureBtn;
@end



@implementation FilterV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self loadUI:frame];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)loadUI:(CGRect)frame
{
    self.hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width / 5, self.height)];
    self.hiddenView.alpha = 0.7;
    self.hiddenView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.hiddenView];
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(self.width / 5, 0, self.width / 5 * 4, self.height)];
    self.backView.alpha = 1.0;
    self.backView.backgroundColor = [UIColor c3Color];
    [self addSubview:self.backView];
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.backView.width - 60) / 2, 30, 60, 20)];
    titleLabel.backgroundColor = [UIColor c3Color];
    titleLabel.textAlignment= NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor c4Color];
    titleLabel.font = [UIFont systemFontOfSize:18];
//    titleLabel.text = @"筛选";
    titleLabel.text = NSLocalizedString(@"Filter", nil);
    [self.backView addSubview:titleLabel];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.backView.width, self.backView.height - 50 - 50) collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor c3Color];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterCell" bundle:nil] forCellWithReuseIdentifier:@"FilterCell"];

    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView"];
    [self.backView addSubview:self.collectionV];

    self.resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [self.resetBtn setTitle:NSLocalizedString(@"Reset", nil) forState:UIControlStateNormal];
    [self.resetBtn setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
    self.resetBtn.backgroundColor = [UIColor c3Color];
    [self.resetBtn addTarget:self action:@selector(resetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:self.resetBtn];
    
    self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn setTitle:NSLocalizedString(@"Confirm", nil) forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.sureBtn.backgroundColor = [UIColor appColor];
    [self.sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:self.sureBtn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.backView.frame = CGRectMake(self.width / 5, 0, self.width / 5 * 4, self.height);
    
    titleLabel.frame = CGRectMake((self.backView.width - 60) / 2, 30, 60, 20);
    
    self.collectionV.frame = CGRectMake(0, 50, self.backView.width, self.backView.height - 50 - 50);
    [self.collectionV reloadData];
    
    self.resetBtn.size = CGSizeMake(self.backView.width / 2, 50);
    self.resetBtn.x = 0;
    self.resetBtn.bottom = self.backView.bottom;
    
    self.sureBtn.size = CGSizeMake(self.backView.width / 2, 50);
    self.sureBtn.x = self.resetBtn.width;
    self.sureBtn.bottom = self.backView.bottom;
}


 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataSource.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    IGArtLibrary *lib = self.dataSource[section];
    return self.dataSource.count ? [lib.lstSubClass count] : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        FilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FilterCell" forIndexPath:indexPath];
        IGArtLibrary *lib = self.dataSource[indexPath.section];
        IGArtDetailLibrary *detailLib = lib.lstSubClass[indexPath.item];
        cell.titleL.text = detailLib.title;
        cell.sel = [detailLib.isSelect isEqualToString:@"1"] ? YES : NO;
        [cell setNeedsLayout];
        return cell;
    }
    return nil;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSArray *arr = self.dataSource[indexPath.section];
//    NSString *str = arr[indexPath.item][@"title"];
//    CGFloat width = [Tool widthForLabel:[NSString stringWithFormat:@"%@",str] fontSize:13];
//    return CGSizeMake(width + 30, 30);
    return CGSizeMake((self.backView.width - 61) / 3, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FilterReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView" forIndexPath:indexPath];
    if (!reusableV)
    {
        reusableV = [[NSBundle mainBundle] loadNibNamed:@"FilterReusableView" owner:self options:nil].firstObject;
    }
    IGArtLibrary *lib = self.dataSource[indexPath.section];
    reusableV.typeL.text = lib.title;
    reusableV.moreBtn.hidden = YES;
//    @KWeakObj(self)
//    reusableV.moreBtn.hidden = [staueArr[indexPath.section] isEqualToString:@"0"] ? YES : NO;
//    reusableV.moreBtn.selected = [staueArr[indexPath.section] isEqualToString:@"2"] ? YES : NO;
//
//    if (indexPath.section == 0)
//    {
//        reusableV.block = ^(UIButton *btn)
//        {
//            btn.selected = !btn.selected;
//            [staueArr replaceObjectAtIndex:indexPath.section withObject:[NSString stringWithFormat:@"%@", btn.selected ? @"2" : @"1"]];;
//            [selfWeak.collectionV reloadData];
//        };
//    }
//    else if (indexPath.section == 3)
//    {
//        reusableV.block = ^(UIButton *btn)
//        {
//            btn.selected = !btn.selected;
//            [staueArr replaceObjectAtIndex:indexPath.section withObject:[NSString stringWithFormat:@"%@", btn.selected ? @"2" : @"1"]];
//            [selfWeak.collectionV reloadData];
//        };
//    }
    return reusableV;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.backView.width, 60);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.filterBlock(self.dataSource);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld---------%ld",(long)indexPath.item,(long)indexPath.section);
    IGArtLibrary *lib = self.dataSource[indexPath.section];
    IGArtDetailLibrary *detailLib = lib.lstSubClass[indexPath.item];
    detailLib.isSelect = [detailLib.isSelect isEqualToString:@"1"] ? @"0" : @"1";
    [self.collectionV reloadItemsAtIndexPaths:@[indexPath]];
}

- (void)resetBtnClick
{
    self.dataSource = [self addSelectStrWithArr:self.dataSource];
    [self.collectionV reloadData];
}

- (void)sureBtnClick
{
    self.sureBlock(self.dataSource);
}

- (NSMutableArray *)addSelectStrWithArr:(NSMutableArray *)arr
{
    NSMutableArray *temp = [arr mutableCopy];
    for (int i = 0; i < arr.count; i++)
    {
        IGArtLibrary *lib = arr[i];
        IGArtLibrary *libTemp = temp[i];
        for (int j = 0; j < lib.lstSubClass.count; j++)
        {
            IGArtDetailLibrary *dic = libTemp.lstSubClass[j];
            dic.isSelect = @"0";
        }
    }
    return temp;
}


@end
